#include <stdio.h>
#include <stdlib.h>

#include "set_bits.h"

struct set* set__empty(struct set* s)
{
  int i;
  for(i=0; i<SET__SIZE; i++)
    s->bits[i] = 0;
  return s;
}

size_t set__size (const struct set * s )
{
  int i,j,res = 0;
  for(i=0;i<SET__SIZE; i++)
    for(j=0; j<32; j++)
      if(s->bits[i] & 1<<j)
        res++;
  return res;
}

int set__is_empty (const struct set * s)
{
  int i;
  for(i=0; i<SET__SIZE; i++)
    if(s->bits[i])
      return 0;
  return 1;
}

struct set* set__union(const struct set * s1, const struct set * s2, struct set * su)
{
  int i;
  for(i=0; i<SET__SIZE; i++)
    su->bits[i] = s1->bits[i] | s2->bits[i];
  return su;
}


struct set* set__add( struct set* s, unsigned char c)
{
  s->bits[c/32] = s->bits[c/32] | 1<<(c%32);
  return s;
}

struct set* set__remove (struct set * s, unsigned char c)
{
    s->bits[c/32] = s->bits[c/32] & !(1<<(c%32));
   return s;
}

int  set__find(const struct set * s, unsigned char c)
{
  if( s->bits[c/32] & 1<<(c%32) )
    return 1;
  else
    return 0;
}

struct set* set__inter(const struct set* s1, const struct set* s2, struct set* res)
{
  int i;
  for(i=0; i<SET__SIZE; i++)
    res->bits[i] = s1->bits[i] & s1->bits[i];
  return res;
}

void set__display(struct set* s)
{
  int i,j;
  for(i=0;i<SET__SIZE; i++)
    for(j=0; j<32; j++)
      if(s->bits[i] & 1<<j)
        printf("%c ", 32*i + j);
}

int compare (const struct set * s1 , const struct set * s2)
{
  int i;
  for(i=0; i<SET__SIZE; i++)
    if(s1->bits[i] != s2->bits[i])
      return 0;
  return 1;
}

struct set* set__copy (const struct set * o, struct set * d)
{
  int i;
  for(i=0; i<SET__SIZE; i++)
    d->bits[i] = o->bits[i];
  return d;
}




















